package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {

    @Autowired
    private CustomerTraceHistoryMapper customerTraceHistoryMapper;

    @Override
    public void delete(Long id) {
        customerTraceHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(CustomerTraceHistory customerTraceHistory) {

        //保存的时候有值是空的

        //设置录入人
        customerTraceHistory.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置录入事件
        customerTraceHistory.setInputTime(new Date());

        customerTraceHistoryMapper.insert(customerTraceHistory);
    }

    @Override
    public CustomerTraceHistory get(Long id) {
        return customerTraceHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTraceHistory> listAll() {
        return customerTraceHistoryMapper.selectAll();
    }

    @Override
    public void update(CustomerTraceHistory customerTraceHistory) {
        customerTraceHistoryMapper.updateByPrimaryKey(customerTraceHistory);
    }

    /**
     *
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(QueryObject qo) {
        //int count = customerTraceHistoryMapper.selectForCount(qo);//高查询时需要参数
        //判断是否有数据
        //if (count == 0) {
        //    return new PageResult(qo.getPageSize());
        //}
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        //查询结果集
        List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectForList(qo);
        //return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
        return new PageInfo(list);
    }
}
